package edu.spec.student.controller;

import edu.spec.student.model.Clazz;
import edu.spec.student.model.ClazzDto;
import edu.spec.student.model.mental.Exam;
import edu.spec.student.model.mental.Mental;
import edu.spec.student.service.iface.IClazzService;
import edu.spec.student.service.iface.IExamService;
import edu.spec.student.service.iface.IMentalService;
import edu.spec.basic.model.dto.MvcReturn;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/exam")
public class ExamController {

    @Inject
    private IExamService examService;
    @Inject
    private IMentalService mentalService;
    @Inject
    private IClazzService clazzService;

    @ResponseBody
    @RequestMapping("/add")
    public MvcReturn add(Exam e, Integer mentalId){
        Mental m = mentalService.load(mentalId);
        e.setMental(m);
        e.setMentalName(m.getName());
        e.setCreateDate(new Date());
        examService.add(e);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/delete/{examId}")
    public MvcReturn delete(@PathVariable Integer examId){
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/find")
    public MvcReturn find(){
        Pager<Exam> pager = examService.find();
        return new MvcReturn(true, pager.getDatas(), (Long)pager.getTotal());
    }

    @ResponseBody
    @RequestMapping("/addClazz/{examId}/{clazzId}")
    public MvcReturn addClazz(@PathVariable Integer examId, @PathVariable Integer clazzId){
        examService.addClazz(examId, clazzId);
        return new MvcReturn(true);
    }

    /**
     * 删除测试对应的某一班级
     * 策略：不推荐使用物理删除，推荐使用disable属性
     * 如果没有StudentExam关联时，可以删除对应的ExamClazz，
     * 如果有了StudentExam的话就不能进行物理删除了，可以通过禁用Exam
     * @param examId
     * @param clazzId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delClazz/{examId}/{clazzId}")
    public MvcReturn delClazz(@PathVariable Integer examId, @PathVariable Integer clazzId){
        // 0、检查是否有ExamStudent，有则msg警告提示，无则直接删除
        // 1、删除
//        examService.deletelClazz(examId, clazzId);
        return new MvcReturn(true);
    }

    /**
     * dto.selected属性记录了改班级是否被选中
     * @param examId
     * @return
     */
    @ResponseBody
    @RequestMapping("/listClazzDto")
    public MvcReturn listClazzDto(Integer examId){
        List<Clazz> allClazz = clazzService.listInSchoolClazz();
        List<Integer> ids = examService.listClazzIdsByExam(examId);

        List<ClazzDto> clazzDtos = new ArrayList<ClazzDto>();
        for(Clazz c : allClazz){
            ClazzDto cd = new ClazzDto();
            cd.setId(c.getId());
            cd.setName(c.getName());
            cd.setStatus(c.getStatus());
            cd.setStudentNumber(c.getStudentNumber());
            cd.setSelected(false);
            for(Integer id : ids){
                if (cd.getId()==id){
                    cd.setSelected(true);
                }
            }
            clazzDtos.add(cd);
        }
        return new MvcReturn(true, clazzDtos);
    }


}